<template>
  <div>
    <a-modal
      ok-text="确认"
      cancel-text="取消"
      :maskClosable="false"
      :title="title"
      :width="modalWidth"
      @cancel="handleClose"
      :destroyOnClose="true"
      @ok="handleOk"
      :visible="visible"
      :confirmLoading="confirmLoading"
      footer=""
    >
      <template>
        <div class="modalBut">
          <a-button @click="handleClose">
            取消
          </a-button>
          <a-button @click="handleOk" type="primary">确认</a-button>
        </div>
      </template>
      <a-spin :spinning="confirmLoading">
        <a-form-model ref="ruleForm" :model="model" :rules="rules">
          <a-form-model-item
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            label="班次"
            hasFeedback
            prop="workTeam"
          >
            <a-select
              :open="false"
              :defaultOpen="false"
              mode="multiple"
              :allowClear="true"
              placeholder="请选择班次"
              v-model.trim="model.workTeam"
              @dropdownVisibleChange="handleTableSelect"
              @change="handleGoodChange"
            >
              <a-select-option
                v-for="item in tableOption"
                :key="item.id"
                :value="item.id"
              >
                {{ item.teamName }}
              </a-select-option>
            </a-select>
          </a-form-model-item>

          <a-form-model-item
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            label="开始时间"
            prop="processStartTime"
            hasFeedback
          >
            <a-date-picker
              :showToday="false"
              placeholder="请选择开始时间"
              @change="
                (date, dateString) =>
                  onChange(date, dateString, 'processStartTime')
              "
              style="width:100%"
              v-model.trim="model.processStartTime"
            />
          </a-form-model-item>
          <a-form-model-item
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            label="备注"
            hasFeedback
            prop="remark"
          >
            <a-input placeholder="请输入备注" v-model.trim="model.remark" />
          </a-form-model-item>
        </a-form-model>
      </a-spin>
    </a-modal>
    <TableSelectModal
      :replaceFields="replaceFields"
      :columns="columns"
      :tableKey="tableKey"
      :name="name"
      :filename="filename"
      ref="tableModal"
      @tableSelect="tableSelect"
    ></TableSelectModal>
  </div>
</template>

<script>
import api from "@/api/apiServer";
import TableSelectModal from "@/components/TableSelectModal";
import { columns } from "@/pages/ShiftMent/components/tableConfig.js";
export default {
  components: { TableSelectModal },
  data() {
    return {
      title: "", // 标题
      visible: false, // 状态、
      modalWidth: 652, // 抽屉宽度
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      // 班次选择组件参数
      replaceFields: {},
      tableKey: "shiftListPage",
      name: "teamName",
      filename: "ShiftMentModal",
      columns,
      tableOption: [],
    };
  },
  computed: {
    rules() {
      return {
        workTeam: [{ required: true, message: "请选择班次" }],
        processStartTime: [{ required: true, message: "请选择开始时间" }],
      };
    },
  },
  methods: {
    onChange(date, dateString, key) {
      this.$set(this.model, key, dateString);
    },
    handleTableSelect() {
      this.$refs.tableModal.title = "班次";
      this.$refs.tableModal.modalAdd();
      if (this.model.workTeam) {
        let arr = this.model.workTeam.toString().split(",");
        if (arr.length > 0) {
          let newArr = arr.map((item) => {
            return Number(item);
          });
          this.$refs.tableModal.selectedRowKeys = newArr;
        } else {
          this.$refs.tableModal.selectedRowKeys = [];
        }
      }
    },
    // 班次选择
    tableSelect(result, tableOption) {
      this.tableOption = tableOption;
      let checkResult = result[0];
      this.$set(this.model, "workTeam", checkResult.id);
      this.$refs.ruleForm.clearValidate(["workTeam"]);
    },
    handleGoodChange(value) {
      if (value.length > 0) {
        this.model.workTeam = value.toString();
      } else {
        delete this.model.workTeam;
      }
    },
    // 新增
    modalAdd() {
      this.modalEdit({});
    },
    // 编辑
    modalEdit(record) {
      this.resetScreenSize();
      this.getTree();
      this.visible = true;
      this.model = Object.assign({}, record);
    },
    async getTree() {
      const res = await api.DepartmentTree();
      this.treeData = res.data;
    },
    async submitVulFun() {
      this.confirmLoading = true;
      let res = await api.WorksheetAutoMatic(this.model);
      if (res.code && res.code !== 200) {
        this.$message.error(res.message);
        this.confirmLoading = false;
        this.model = Object.assign({}, this.model);
        return;
      }
      this.$message.success(res.message);
      this.confirmLoading = false;
      this.$emit("ok");
      this.visible = false;
    },
    handleOk() {
      const that = this;
      // 触发表单验证
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          that.submitVulFun();
        } else {
          return false;
        }
      });
    },
    // 关闭
    handleClose() {
      this.visible = false;
      this.model = Object.assign({});
      this.$refs.ruleForm.resetFields();
    },
    // 根据屏幕变化,设置抽屉尺寸
    resetScreenSize() {
      let screenWidth = document.body.clientWidth;
      if (screenWidth < 500) {
        this.modalWidth = screenWidth;
      } else {
        this.modalWidth = 652;
      }
    },
  },
};
</script>

